package com.roc.web.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


/**
 * Application Lifecycle Listener implementation class RootPathListener
 * 
 */
public class RootPathListener implements ServletContextListener {

	static Logger logger = LogManager.getLogger(RootPathListener.class);

	/**
	 * Default constructor.
	 */
	public RootPathListener() {
		logger.info("----------------RootPathListener---------------");
	}

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent sc) {
		logger.info("----------------setRootPath---------------");
//		String rootPath = sc.getServletContext().getContextPath();
		String rootPath = sc.getServletContext().getInitParameter("rootPath");
		if(StringUtils.isBlank(rootPath)){
			rootPath =sc.getServletContext().getContextPath();
			System.out.println("-------------------"+rootPath);
		}
//		String rootPath = sc.getServletContext().getAttribute("")
//		Enumeration e =sc.getServletContext().getAttributeNames();
//		while(e.hasMoreElements()){
//			Object o = e.nextElement();
			//logger.debug(o+"="+sc.getServletContext().getAttribute(o.toString()));
//			System.out.println(o+"="+sc.getServletContext().getAttribute(o.toString()));
//		}
		logger.info("------rootPath:" + rootPath+"----------");
		sc.getServletContext().setAttribute("rootPath", rootPath);
		
		
	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent sc) {
		sc.getServletContext().removeAttribute("rootPath");
		logger.info("----------------RootPathListener Destroyed---------------");
	}
}
